home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Numerical / Lib / UserArray.py < prev   
Encoding:
Python Source  |  2000-06-23  |  2.9 KB  |  104 lines

  1. from Numeric import *
  2. import string
  3.  
  4. class UserArray:
  5.     def __init__(self, data, typecode = None):
  6.         self.array = array(data, typecode)
  7.         self.shape = self.array.shape
  8.         self._typecode = self.array.typecode()
  9.         self.name = string.split(str(self.__class__))[0]
  10.  
  11.     def __repr__(self):
  12.         return self.name+repr(self.array)[len("array"):]
  13.  
  14.     def __array__(self,t=None):
  15.         if t: return asarray(self.array,t)
  16.         return asarray(self.array)
  17.  
  18.     def __float__(self):
  19.         return float(asarray(self.array))
  20.  
  21.     # Array as sequence
  22.     def __len__(self): return len(self.array)
  23.  
  24.     def __getitem__(self, index): 
  25.         return self._rc(self.array[index])
  26.  
  27.     def __getslice__(self, i, j): 
  28.         return self._rc(self.array[i:j])
  29.  
  30.  
  31.     def __setitem__(self, index, value): self.array[index] = asarray(value,self._typecode)
  32.     def __setslice__(self, i, j, value): self.array[i:j] = asarray(value,self._typecode)
  33.  
  34.     def __del__(self):
  35.         for att in dir(self):
  36.             delattr(self,att)
  37.             #del(self)
  38.  
  39.     def __abs__(self): return self._rc(absolute(self.array))
  40.     def __neg__(self): return self._rc(-self.array)
  41.  
  42.     def __add__(self, other): 
  43.         return self._rc(self.array+asarray(other))
  44.     __radd__ = __add__
  45.  
  46.     def __sub__(self, other): 
  47.         return self._rc(self.array-asarray(other))
  48.     def __rsub__(self, other): 
  49.         return self._rc(asarray(other)-self.array)
  50.  
  51.     def __mul__(self, other): 
  52.         return self._rc(multiply(self.array,asarray(other)))
  53.     __rmul__ = __mul__
  54.  
  55.     def __div__(self, other): 
  56.         return self._rc(divide(self.array,asarray(other)))
  57.     def __rdiv__(self, other): 
  58.         return self._rc(divide(asarray(other),self.array))
  59.  
  60.     def __pow__(self,other): 
  61.         return self._rc(power(self.array,asarray(other)))
  62.     def __rpow__(self,other): 
  63.         return self._rc(power(asarray(other),self.array))
  64.  
  65.     def __sqrt__(self): 
  66.         return self._rc(sqrt(self.array))
  67.  
  68.     def tostring(self): return self.array.tostring()
  69.  
  70.     def byteswapped(self): return self._rc(self.array.byteswapped())
  71.     def astype(self, typecode): return self._rc(self.array.asType(typecode))
  72.    
  73.     def typecode(self): return self.array._typecode
  74.     def itemsize(self): return self.array.itemsize()
  75.     def iscontiguous(self): return self.array.iscontiguous()
  76.  
  77.     def _rc(self, a):
  78.         if len(shape(a)) == 0: return a
  79.         else: return self.__class__(a)
  80.  
  81.  
  82. #############################################################
  83. # Test of class UserArray
  84. #############################################################
  85. if __name__ == '__main__':
  86.     import Numeric
  87.  
  88.     temp=reshape(arange(10000),(100,100))
  89.  
  90.     ua=UserArray(temp)
  91.     # new object created begin test
  92.     print dir(ua)
  93.     print shape(ua),ua.shape # I have changed Numeric.py
  94.  
  95.     ua_small=ua[:3,:5]
  96.     print ua_small
  97.     ua_small[0,0]=10  # this did not change ua[0,0], wich is not normal behavior
  98.     print ua_small[0,0],ua[0,0]
  99.     print sin(ua_small)/3.*6.+sqrt(ua_small**2)
  100.     print less(ua_small,103),type(less(ua_small,103))
  101.     print type(ua_small*reshape(arange(15),shape(ua_small)))
  102.     print reshape(ua_small,(5,3))
  103.     print transpose(ua_small)
  104.